package com.ruoyi.project.cms.notice.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.notice.domain.Type;
import com.ruoyi.project.cms.notice.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/notice/type")
public class TypeController extends BaseController {

    @Autowired
    private TypeService  typeService;


    String  prefix="/cms/notice/type";

    @GetMapping("")
    public String type()
    {
        return  prefix+ "/type";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Type type){
        startPage();

        List<Type> list = typeService.findMore(type);

        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @PostMapping("/typeAdd")
    @ResponseBody
    public AjaxResult typeAdd(Type  type)
    {

        return toAjax(typeService.save(type));
    }

    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public   String   checkTypeNameUnique(Type  type){

        if (type.getTypeId() != null) {
            return typeService.findType(type);
        }
        return typeService.findTypeName(type.getTypeName());
    }

    /**
     * 修改公告类型
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Integer typeId, ModelMap mmap)
    {
        mmap.put("type", typeService.findTypeById(typeId));
        return prefix + "/edit";
    }

    /**
     * 保存修改后的公告类型
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Type type)
    {
        return toAjax(typeService.updateType(type));
    }

    /**
     * 查看公告类型
     */
    @GetMapping("/selType/{typeId}")
    public String selType(@PathVariable("typeId") Integer typeId, ModelMap mmap)
    {
        mmap.put("type", typeService.findTypeById(typeId));
        return prefix + "/info";
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeType(String ids)
    {
        try
        {
            return toAjax(typeService.removeType(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    @PostMapping("/selDesc")
    @ResponseBody
    public AjaxResult selDesc()
    {
        return toAjax(1);
    }
}
